{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Denoising Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "import time\n",
    "import brainflow\n",
    "import numpy as np\n",
    "\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds\n",
    "from brainflow.data_filter import DataFilter, FilterTypes, AggOperations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use synthetic board for demo\n",
    "params = BrainFlowInputParams ()\n",
    "board_id = BoardIds.SYNTHETIC_BOARD.value\n",
    "board = BoardShim (board_id, params)\n",
    "board.prepare_session ()\n",
    "board.start_stream ()\n",
    "time.sleep (20)\n",
    "data = board.get_board_data ()\n",
    "board.stop_stream ()\n",
    "board.release_session ()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot original data\n",
    "eeg_channels = BoardShim.get_eeg_channels (board_id)\n",
    "df = pd.DataFrame (np.transpose (data))\n",
    "df[eeg_channels].plot (subplots = True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# demo for different denoising methods, \n",
    "# apply different methods to different channels to determine the best one\n",
    "for count, channel in enumerate (eeg_channels):\n",
    "    # first of all you can try simple moving median or moving average with different window size\n",
    "    if count == 0:\n",
    "        DataFilter.perform_rolling_filter (data[channel], 3, AggOperations.MEAN.value)\n",
    "    elif count == 1:\n",
    "        DataFilter.perform_rolling_filter (data[channel], 3, AggOperations.MEDIAN.value)\n",
    "    # methods above should increase signal to noise ratio but we can do even better\n",
    "    # using wavelet based denoising, feel free to try different wavelet functions and decomposition levels\n",
    "    elif count == 2:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'db6', 5)\n",
    "    elif count == 3:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'bior3.9', 5)\n",
    "    elif count == 4:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'sym7', 5)\n",
    "    elif count == 5:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'coif3', 5)\n",
    "    elif count == 6:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'bior6.8', 5)\n",
    "    elif count == 7:\n",
    "        DataFilter.perform_wavelet_denoising (data[channel], 'db4', 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot denoised data\n",
    "df = pd.DataFrame (np.transpose (data))\n",
    "df[eeg_channels].plot (subplots = True)\n",
    "plt.show()\n",
    "# as you can see wavelet based denoising works much better and increases signal to noise ratio significantly!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
